﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using System.Xml.Linq;
using System.Windows.Documents;
using System.IO;
using Agnes.Core;
using Agnes.Libaray;
using System.Windows;
using Csla.Rules.CommonRules;

namespace Agnes.MyDocsLib
{
    /// <summary>
    /// 文章Model
    /// </summary>
    [Serializable]
    public class Article : ModelBase<Article>, IArticleNode
    {
        #region Property

        /// <summary>
        /// 文章所属分类
        /// </summary>
        public Catalog ParentCatalog { get; set; }

        #endregion

        #region Csla Property

        public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(c => c.Id);
        /// <summary>
        /// 文章的唯一ID
        /// </summary>
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set
            {
                SetProperty(IdProperty, value);
            }
        }

        public static readonly PropertyInfo<string> TitleProperty = RegisterProperty<string>(c => c.Title);
        /// <summary>
        /// 文章标题
        /// </summary>
        public string Title
        {
            get { return GetProperty(TitleProperty); }
            set { SetProperty(TitleProperty, value); }
        }


        public static readonly PropertyInfo<string> LabelsProperty = RegisterProperty<string>(c => c.Labels);
        /// <summary>
        /// 文章标签(各个标签使用;隔开)
        /// </summary>
        public string Labels
        {
            get { return GetProperty(LabelsProperty); }
            set { SetProperty(LabelsProperty, value); }
        }

        public static readonly PropertyInfo<DateTime> CreateDTProperty = RegisterProperty<DateTime>(c => c.CreateDT);
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateDT
        {
            get { return GetProperty(CreateDTProperty); }
            set { SetProperty(CreateDTProperty, value); }
        }

        public static readonly PropertyInfo<DateTime> UpdateDTProperty = RegisterProperty<DateTime>(c => c.UpdateDT);
        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdateDT
        {
            get { return GetProperty(UpdateDTProperty); }
            set { SetProperty(UpdateDTProperty, value); }
        }

        public static readonly PropertyInfo<int> PreviewCountProperty = RegisterProperty<int>(c => c.PreviewCount);
        /// <summary>
        /// 预览次数
        /// </summary>
        public int PreviewCount
        {
            get { return GetProperty(PreviewCountProperty); }
            set { SetProperty(PreviewCountProperty, value); }
        }

        public static readonly PropertyInfo<string> CatalogPathProperty = RegisterProperty<string>(c => c.CatalogPath);
        /// <summary>
        /// 目录路径
        /// </summary>
        public string CatalogPath
        {
            get { return GetPath(); }
            private set { LoadProperty(CatalogPathProperty, value); }
        }

        #endregion

        #region Csla Method

        /// <summary>
        /// DoCreate
        /// </summary>
        /// <param name="createParam"></param>
        protected override void DoCreate(object createParam)
        {
            base.DoCreate(createParam);
            using (BypassPropertyChecks)
            {
                Id = Guid.NewGuid();
                Title = "未命名文章";
                ParentCatalog = null;
                CreateDT = DateTime.Now;
                UpdateDT = DateTime.Now;
                Labels = "";
                PreviewCount = 0;
                DocumentPath = Path.Combine(AgnesServiceManager.AgnesSlnService.CurrentSln.TempDir, "Date_MyDocsLib", Id + ".mht");
            }
        }

        /// <summary>
        /// DoFetch
        /// </summary>
        /// <param name="fetchParam"></param>
        protected override void DoFetch(object fetchParam)
        {
            base.DoFetch(fetchParam);
            using (BypassPropertyChecks)
            {
                XElement el = fetchParam as XElement;
                Id = Guid.Parse(el.Attribute("Id").Value);
                Title = el.Attribute("Title").Value;
                CreateDT = DateTime.Parse(el.Attribute("CreateDT").Value);
                UpdateDT = DateTime.Parse(el.Attribute("UpdateDT").Value);
                Labels = el.Attribute("Labels").Value;
                PreviewCount = Int32.Parse(el.Attribute("PreviewCount").Value);
                DocumentPath = Path.Combine(AgnesServiceManager.AgnesSlnService.CurrentSln.TempDir, "Date_MyDocsLib", Id + ".mht");
            }
            DataPortal_Update();
        }

        private XElement GetXml()
        {
            XElement el = new XElement("Article",
                new XAttribute("Id", Id),
                new XAttribute("Title", Title),
                new XAttribute("Labels", Labels),
                new XAttribute("CreateDT", CreateDT.ToShortTimeString()),
                new XAttribute("UpdateDT", UpdateDT.ToShortTimeString()),
                new XAttribute("PreviewCount", PreviewCount));
            return el;
        }

        /// <summary>
        /// SaveToXml
        /// </summary>
        /// <param name="Save"></param>
        public void SaveToXml(Action<XElement> Save)
        {
            DataPortal.UpdateChild(this);
            Save(GetXml());
        }

        #endregion

        #region Validation

        protected override void AddBusinessRules()
        {
            base.AddBusinessRules();
            AddTitleRule();
        }

        private void AddTitleRule()
        {
            var r = new Lambda(TitleProperty, t =>
            {
                Article h = t.Target as Article;
                if (string.IsNullOrEmpty(h.Title))
                {
                    t.AddErrorResult(TitleProperty, "标题不能为空！");
                }
            });

            BusinessRules.AddRule(r);
        }

        #endregion

        #region Method

        public string DocumentPath { get; private set; }

        /// <summary>
        /// GetPath
        /// </summary>
        /// <returns></returns>
        private string GetPath()
        {
            string res = "";

            Catalog tmp = ParentCatalog;
            while (tmp != null)
            {
                res = tmp.Title + "/" + res;
                tmp = tmp.ParentCatalog;
            }

            res = "我的知识库/" + res;
            return res;
        }

        #endregion

        /// <summary>
        /// 设置为未保存状态
        /// </summary>
        public void MakeDirty()
        {
            base.MarkDirty();
        }
    }

}
 